﻿using JewererBasic.BO;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JewererBasic.Controllers
{
    public class CommonController : Controller
    {
        public JsonResult GetProductProcess()
        {
            MSQDBEntities db = new MSQDBEntities();
            var result = from d in db.PROCESSes
                         where d.Type == 1
                         select d.Name;
            return Json(new { Processes = result.ToList() }, JsonRequestBehavior.AllowGet);
        }
    
        private CommonServiceImp commonService;

        public CommonController()
        {
            this.commonService = new CommonServiceImp();
        }

        public ActionResult AutoCompleteMould(string query = null)
        {
            return Json(commonService.AutoCompleteMould(query), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteStone(string query = null)
        {
            return Json(commonService.AutoCompleteStone(query), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteComponent(string query = null)
        {
            return Json(commonService.AutoCompleteComponent(query), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteProcess(string query, TypeProcess type)
        {
            var lst = commonService.AutoCompleteProcess(query, type);
            List<Product_Process> result = new List<Product_Process>();
            foreach (var item in lst)
            {
                result.Add(new Product_Process() { ProcessName = item.Name, IdProcess = item.Id, Time = 0 });
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteType(string query = null)
        {
            return Json(commonService.AutoCompleteType(query), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AutoCompleteUnit(string query = null)
        {
            return Json(commonService.AutoCompleteUnit(query), JsonRequestBehavior.AllowGet);
        }
    }  
}
